"""
基础异常定义
===========
定义所有异常的基类和通用异常
"""

from typing import Any, Dict, Optional


class RedFireException(Exception):
    """RedFire系统基础异常"""
    
    def __init__(self, message: str, error_code: str = None, details: Dict[str, Any] = None):
        super().__init__(message)
        self.message = message
        self.error_code = error_code or self.__class__.__name__.upper()
        self.details = details or {}
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            'error_code': self.error_code,
            'message': self.message,
            'details': self.details,
            'exception_type': self.__class__.__name__
        }


class ValidationException(RedFireException):
    """验证异常"""
    
    def __init__(self, message: str = "数据验证失败", field: str = None, value: Any = None):
        details = {}
        if field:
            details['field'] = field
        if value is not None:
            details['value'] = value
        
        super().__init__(
            message=message,
            error_code="VALIDATION_ERROR",
            details=details
        )


class AuthenticationException(RedFireException):
    """认证异常"""
    
    def __init__(self, message: str = "认证失败"):
        super().__init__(
            message=message,
            error_code="AUTHENTICATION_ERROR"
        )


class AuthorizationException(RedFireException):
    """授权异常"""
    
    def __init__(self, message: str = "权限不足", required_permission: str = None):
        details = {}
        if required_permission:
            details['required_permission'] = required_permission
        
        super().__init__(
            message=message,
            error_code="AUTHORIZATION_ERROR",
            details=details
        )


class ConfigurationException(RedFireException):
    """配置异常"""
    
    def __init__(self, message: str = "配置错误", config_key: str = None):
        details = {}
        if config_key:
            details['config_key'] = config_key
        
        super().__init__(
            message=message,
            error_code="CONFIGURATION_ERROR",
            details=details
        )


class ResourceNotFoundException(RedFireException):
    """资源未找到异常"""
    
    def __init__(self, message: str = "资源未找到", resource_type: str = None, resource_id: str = None):
        details = {}
        if resource_type:
            details['resource_type'] = resource_type
        if resource_id:
            details['resource_id'] = resource_id
        
        super().__init__(
            message=message,
            error_code="RESOURCE_NOT_FOUND",
            details=details
        )


class ConflictException(RedFireException):
    """冲突异常"""
    
    def __init__(self, message: str = "资源冲突", resource_type: str = None):
        details = {}
        if resource_type:
            details['resource_type'] = resource_type
        
        super().__init__(
            message=message,
            error_code="RESOURCE_CONFLICT",
            details=details
        )


class RateLimitException(RedFireException):
    """限流异常"""
    
    def __init__(self, message: str = "请求频率过高", retry_after: int = None):
        details = {}
        if retry_after:
            details['retry_after'] = retry_after
        
        super().__init__(
            message=message,
            error_code="RATE_LIMIT_EXCEEDED",
            details=details
        )
